/**
 * jims
 */
package com.jims.master.modules.csm.service;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.IdGen;
import com.jims.master.modules.csm.dao.ExpInoutClassDao;
import com.jims.master.modules.csm.dao.ExpStorageDeptDao;
import com.jims.master.modules.csm.dao.ExpSupplierCataDao;
import com.jims.master.modules.csm.entity.ExpInoutClass;
import com.jims.master.modules.csm.entity.ExpStorageDept;
import com.jims.master.modules.csm.entity.ExpSupplierCata;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 消耗品厂商Entity
 * @author DT
 * @version 2017-03-01
 */
@Service
@Transactional(readOnly = true)
public class ExpSupplierCataService extends CrudService<ExpSupplierCataDao, ExpSupplierCata> {

    @Autowired
    private ExpStorageDeptDao expStorageDeptDao;
    @Autowired
    private ExpInoutClassDao expInoutClassDao;

	public ExpSupplierCata get(String id) {
		return super.get(id);
	}

	/**
	 * 消耗品基本信息调用厂商自动补全方法
	 * @param expSupplierCata
	 * @param q
	 * @param limit
	 * @return
	 * @author CTQ
	 * @date 2017-03-09 15:08:59
	 */
	public List<ExpSupplierCata> findAutoCompleteList(ExpSupplierCata expSupplierCata,String q,String limit,String supplierClass) {
		return dao.findAutoCompleteList(expSupplierCata, q, limit,supplierClass);
	}

	public Page<ExpSupplierCata> findPage(Page<ExpSupplierCata> page, ExpSupplierCata expSupplierCata) {
		return super.findPage(page, expSupplierCata);
	}

	@Transactional(readOnly = false)
	public int save(ExpSupplierCata expSupplierCata) {
        int num = 0;
        if(null == expSupplierCata.getId() || expSupplierCata.getId().trim().equals("")){
            expSupplierCata.preInsert();
            expSupplierCata.setId(IdGen.uuid());
            num = dao.insert(expSupplierCata);
        }else{
            expSupplierCata.preUpdate();
            num = dao.update(expSupplierCata);
        }
		return num;
	}

	@Transactional(readOnly = false)
	public int delete(ExpSupplierCata expSupplierCata) {
		return super.delete(expSupplierCata);
	}

    /**
     * 根据传过来的入(出)库类别返回供货方(收货方)集合
     * @param inoutType 入库 1； 出库 0
     * @param type      类别：如采购、退货(传过来的是对应的代码数字,而不是名称汉字)
     * @return
     * @author fengyg
     */
    public List getSupplierOrStorageList(String inoutType, String type) {
        String storageLevel = "";
        ExpStorageDept thisStorage = expStorageDeptDao.get(UserUtils.getUser().getOrgDept().getId());
        if(null != thisStorage){
            storageLevel = thisStorage.getStorageLevel();  //获取当前库房级别
        }else{
            return new ArrayList();
        }
        ExpStorageDept expStorageDept1 = new ExpStorageDept();
        expStorageDept1.setOrgId(UserUtils.getOrgId());
        expStorageDept1.setStorageLevel("1");
        expStorageDept1.setDelFlag("0");
        List<ExpStorageDept> list1 = expStorageDeptDao.getByStorageLevel(expStorageDept1);  //二级库
        ExpStorageDept expStorageDept2 = new ExpStorageDept();
        expStorageDept2.setOrgId(UserUtils.getOrgId());
        expStorageDept2.setStorageLevel("2");
        expStorageDept2.setDelFlag("0");
        List<ExpStorageDept> list2 = expStorageDeptDao.getByStorageLevel(expStorageDept2);  //三级库(科室库)
        ExpStorageDept expStorageDept3 = new ExpStorageDept();
        expStorageDept3.setOrgId(UserUtils.getOrgId());
        expStorageDept3.setStorageLevel("0");
        expStorageDept3.setDelFlag("0");
        List<ExpStorageDept> list3 = expStorageDeptDao.getByStorageLevel(expStorageDept3);  //一级库

        ExpInoutClass expInoutClass = new ExpInoutClass();
        expInoutClass.setInoutType(inoutType);
        expInoutClass.setType(type);
        expInoutClass.setStorageType((Integer.parseInt(storageLevel) + 1) + "");
        List<ExpInoutClass> classList = expInoutClassDao.findByStorageLevelInOut(expInoutClass);
        String fromLevel = classList.get(0).getFromLevel();  //来源等级
        if(fromLevel.equals("3")){      //来源等级：供应商
            ExpSupplierCata expSupplierCata = new ExpSupplierCata();
            expSupplierCata.setOrgId(UserUtils.getOrgId());
            expSupplierCata.setDelFlag("0");
            List<ExpSupplierCata> list = dao.findList(expSupplierCata);
            return list;
        }else if(fromLevel.equals("2")){  //来源等级：下级
            if(storageLevel.equals("0")){   //当前库房：一级库
                List<ExpStorageDept> newList = new ArrayList<ExpStorageDept>();
                newList.addAll(list1);
                newList.addAll(list2);
                return newList;
            }else if(storageLevel.equals("1")){  //当前库房：二级库
                List<ExpStorageDept> newList = new ArrayList<ExpStorageDept>();
                newList.addAll(list2);
                return newList;
            }else{  //当前库房：三级库。没有下级库房，返回空集合
                return null;
            }
        }else if(fromLevel.equals("1")){   //来源等级：平级
            //来源等级为平级的类别是：盘点、调拨、建账
            if(type.equals("2") || type.equals("4")){  //建账 2；盘点 4
                List<ExpStorageDept> list = new ArrayList<ExpStorageDept>();
                //类别为盘点或建账，那么供货单位(收货方)为当前库房
                ExpStorageDept expStorageDept = expStorageDeptDao.get(UserUtils.getUser().getOrgDept().getId());
                list.add(expStorageDept);
                return list;
            }else if(type.equals("5")){  //调拨 5
                //类别为调拨，那么供货单位(收货方)为当前库房级别的同级库房(本库房除外)
                ExpStorageDept expStorageDept = new ExpStorageDept();
                expStorageDept.setStorageLevel(storageLevel);
                expStorageDept.setOrgId(UserUtils.getOrgId());
                expStorageDept.setDelFlag("0");
                List<ExpStorageDept> newList = new ArrayList<ExpStorageDept>();
                List<ExpStorageDept> list = expStorageDeptDao.getByStorageLevel(expStorageDept);  //查询当前库房的同级库房
                for (ExpStorageDept storageDept : list) {
                    if(!storageDept.getStorageCode().equals(UserUtils.getUser().getOrgDept().getId())){
                        newList.add(storageDept);
                    }
                }
                return newList;
            }else{
                return null;
            }
        }else if(fromLevel.equals("0")){  //来源等级：上级
            if(storageLevel.equals("2")){  //当前库房：三级库(科室库)
                List<ExpStorageDept> newList = new ArrayList<ExpStorageDept>();
                newList.addAll(list1);
                newList.addAll(list3);
                return newList;
            }else if(storageLevel.equals("1")){   //当前库房：二级库
                List<ExpStorageDept> newList = new ArrayList<ExpStorageDept>();
                newList.addAll(list3);
                return newList;
            }else{   //一级库，没有上级库房，返回空集合
                return null;
            }
        }
        return null;
    }
    public List<ExpSupplierCata> getListBySupplierName(String supplierName,String id){
        return dao.getListBySupplierName(supplierName,id);
    }
}